package com.customer.service.coupon;

import com.customer.mapper.CouponMapper;
import com.customer.model.TblCoupon;
import com.customer.model.TblCouponSendInfo;
import com.customer.model.vo.CouponSendInfoVO;
import com.customer.model.vo.CouponVO;
import com.customer.util.ResponseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 2 * @Author: xy
 * 3 * @Date: 2021/10/28 下午 3:14:06
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Override
    public ResponseData findAllByPage(int page, int limit, String couponName) {
        // 开启分页
        PageHelper.startPage(page, limit);
        // 使用mapper查询
        List<CouponVO> coupons = couponMapper.findAllByConditions(couponName);
        // 1.把所有字段放在一个类中 2.Map 3.使用A类{ B类 }
        PageInfo pageInfo = new PageInfo(coupons);
        return ResponseData.success(pageInfo);
    }

    @Override
    public ResponseData addCoupon(TblCoupon tblCoupon) {
        // 新增的业务
//        1. 当effective_type 值为1时 需要设置 结束时间
        if (tblCoupon.getEffectiveType() == 1) {
            // 结束时间的设置
            tblCoupon.setExpireTime(new Date(tblCoupon.getEnableTime().getTime() + tblCoupon.getEffectiveDay() * 24 * 60 * 60 * 1000));
        }
//        2. create_user当前登录系统管理员
        int count = couponMapper.insert(tblCoupon);
        if (count > 0) {
            return ResponseData.success();
        }
        return ResponseData.fail("新增失败，请重试!");
    }

    @Override
    public ResponseData findAllSendByPage(int page, int limit) {
        // 开启分页
        PageHelper.startPage(page, limit);
        // 使用mapper查询
        List<CouponSendInfoVO> couponSendInfos = couponMapper.findAllSend();
        // 1.把所有字段放在一个类中 2.Map 3.使用A类{ B类 }
        PageInfo pageInfo = new PageInfo(couponSendInfos);
        return ResponseData.success(pageInfo);
    }

    @Override
    public ResponseData add(MultipartFile multipartFile, TblCouponSendInfo tblCouponSendInfo) {
        if (multipartFile == null || multipartFile.isEmpty()) {
            return ResponseData.fail("无上传文件");
        } else {
            Map<String, Object> map = new HashMap<>();
            // 设置文件名称
            map.put("fileame", multipartFile.getName());
            // 设置文件类型
            map.put("contentType", multipartFile.getContentType());
            // 设置文件大小
            map.put("fileSize", multipartFile.getSize());
            // 创建文件名称
            String fileName = UUID.randomUUID() + "."
                    + Objects.requireNonNull(multipartFile.getContentType()).substring(multipartFile.getContentType().lastIndexOf("/") + 1);
            // 获取到文件的路径信息
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
            assert servletRequestAttributes != null;
            String filePath = servletRequestAttributes.getRequest().getServletContext().getRealPath("/") + fileName;
//                    servletRequestAttributes.getRequest().getServletContext().getRealPath("/img") + fileName;
            // 打印保存路径
            map.put("filename", fileName);
            System.out.println(filePath);
            // 保存文件的路径信息
            map.put("filePath", filePath);
            // 创建文件
            File saveFile = new File(filePath);
            // 文件保存
            try {
                multipartFile.transferTo(saveFile);
            } catch (IOException e) {
                e.printStackTrace();
                // 返回信息
                return ResponseData.fail(map);
            }
            // 如果没有插入数据，说明失败
            int count = couponMapper.add(tblCouponSendInfo);
            tblCouponSendInfo.setStatus(1);
            if (count > 0) {

                return ResponseData.fail("添加成功");
            } else {
                return ResponseData.fail();
            }
        }
    }
}
